

use "$path\Intermediary Data\localshock_dataforReg_v2.dta",clear


drop if prix_ht==.
drop j j3
replace d_dieselr=resid_movav_diesel
xtset id daten
gen d_dieselrott=ln(diesel_rotterdam_euro)-ln(l1.diesel_rotterdam_euro)
drop diesel_rotterdam_euro resid_movav_diesel dprix1 

drop d_dieselr
gen d_dieselr=d_dieselrott

merge m:m id using  "$path\Intermediary Data\base_id_suff_stat_6Y"
keep if _m==3
drop _m

merge m:m id using  "$path\Intermediary Data\base_base_suff_stat_6Y_forinteractkurtfreq"
keep if _m==3
drop _m



capture drop ratio
gen ratio=kurtosis/freq

bysort id: gen first_obs=1 if _n==1

egen mean_kur_temp=mean(kur) if first_obs==1
egen mean_freq_temp=mean(freq) if first_obs==1
egen mean_kur=mean(mean_kur_temp)
egen mean_freq=mean(mean_freq_temp)
drop mean_kur_temp mean_freq_temp first_obs

gen kur_demean=kur/mean_kur
gen freq_demean=freq/mean_freq

drop num_obs
gen num_obs=_n-1

capture drop local_shock5 local_shock15 local_shock4 local_shock6 local_shock7 local_shock8
capture drop coeff1_reg
capture drop coeff2_reg
capture drop coeff3_reg
capture drop coeff_c_reg

capture drop se_coeff1_reg
capture drop se_coeff2_reg
capture drop se_coeff3_reg
capture drop df_reg


gen coeff1_reg=.
gen coeff2_reg=.
gen coeff3_reg=.
gen coeff_c_reg=.

gen se_coeff1_reg=.
gen se_coeff2_reg=.
gen se_coeff3_reg=.
gen df_reg=.

capture drop f_prix_ht
gen f_prix_ht=0


foreach i of numlist 1/30 {
 
bysort id (daten):  gen f_prix_ht_temp=(log(prix_ht[_n+`i'])-log(prix_ht[_n-1]))/lt_effect
replace f_prix_ht=f_prix_ht_temp+f_prix_ht



areg f_prix_ht c.d_dieselr##(c.freq_demean c.kur_demean) c.local_shock10##(c.freq_demean c.kur_demean) c.m_dprix##(c.freq_demean c.kur_demean) c.l.d_dieselr##(c.freq_demean c.kur_demean) ///
 c.l2.d_dieselr##(c.freq_demean c.kur_demean) c.l3.d_dieselr##(c.freq_demean c.kur_demean) c.l4.d_dieselr##(c.freq_demean c.kur_demean) c.l5.d_dieselr##(c.freq_demean c.kur_demean) ///
		c.l.local_shock10##(c.freq_demean c.kur_demean)  c.l2.local_shock10##(c.freq_demean c.kur_demean) c.l3.local_shock10##(c.freq_demean c.kur_demean) c.l4.local_shock10##(c.freq_demean c.kur_demean) ///
		c.l5.local_shock10##(c.freq_demean c.kur_demean)	c.l.m_dprix##(c.freq_demean c.kur_demean)  c.l2.m_dprix##(c.freq_demean c.kur_demean) c.l3.m_dprix##(c.freq_demean c.kur_demean) ///
		c.l4.m_dprix##(c.freq_demean c.kur_demean) c.l5.m_dprix##(c.freq_demean c.kur_demean), absorb(id) vce(cluster daten)

	

	
matrix test=r(table)
replace coeff1_reg=_b[d_dieselr] if num_obs==`i'
replace coeff2_reg=_b[c.d_dieselr#c.freq_demean] if num_obs==`i'
replace coeff3_reg=_b[c.d_dieselr#c.kur_demean] if num_obs==`i'
replace coeff_c_reg=_b[_cons] if num_obs==`i'


replace se_coeff1_reg=_se[d_dieselr] if num_obs==`i'
replace se_coeff2_reg=_se[c.d_dieselr#c.freq_demean] if num_obs==`i'
replace se_coeff3_reg=_se[c.d_dieselr#c.kur_demean] if num_obs==`i'
replace df_reg=e(df_r) if num_obs==`i'
capture drop  f_prix_ht_temp
di "Numéro itération :" `i'
}

preserve
keep coeff1_reg coeff2_reg coeff3_reg coeff_c_reg  se_coeff1_reg se_coeff2_reg se_coeff3_reg df_reg num_obs
gen name="BASELINE - interaction kurt freq"
gen charact="LAGS, STANDARD SHOCK, STATION FE, DATE CLUSTER"
keep if num_obs<=40
save "$path\Graphs_Tables\Data_Figure7b.dta", replace
restore



use  "$path\Graphs_Tables\Data_Figure7b.dta", clear

gen ci_coeff1_m=coeff1_reg-1.96*se_coeff1_reg
gen ci_coeff1_p=coeff1_reg+1.96*se_coeff1_reg
gen ci_coeff2_m=coeff2_reg-1.96*se_coeff2_reg
gen ci_coeff2_p=coeff2_reg+1.96*se_coeff2_reg
gen ci_coeff3_m=coeff3_reg-1.96*se_coeff3_reg
gen ci_coeff3_p=coeff3_reg+1.96*se_coeff3_reg

local nj=30
gen h=num_obs
sort h
graph twoway (rarea  ci_coeff1_m ci_coeff1_p h if h<=`nj', col(grey%10)) ///
(line  coeff1_reg h if h<=`nj', col(black) ///
legend(off) graphregion(color(white)) xtitle("Time (in days)") ytitle("Cum. Response")) ///
, xlabel(0(2)`nj') ylabel(0(5)30) 

graph export "$path\Graphs_Tables\Figure7b_1.png", replace
graph export "$path\Graphs_Tables\Figure7b_1.eps", replace


local nj=30
sort h
graph twoway (rarea  ci_coeff2_m ci_coeff2_p h if h<=`nj', col(grey%10)) ///
(line  coeff2_reg h if h<=`nj', col(black) ) ///
(rarea  ci_coeff3_m ci_coeff3_p h if h<=`nj', col(grey%10)) ///
(line coeff3_reg h if h<=`nj', col(black) lpat(dash) ///
legend(off) graphregion(color(white)) xtitle("Time (in days)") ytitle("Cum. Response")) ///
, xlabel(0(2)`nj') 

graph export "$path\Graphs_Tables\Figure7b_2.png", replace
graph export "$path\Graphs_Tables\Figure7b_2.eps", replace

